{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"color:#777777;background-color:#ffffff;font-size:12px;text-align:right;\">\n",
    "\tprepared by Abuzer Yakaryilmaz (QuSoft@Riga) | November 07, 2018\n",
    "</div>\n",
    "<table><tr><td><i> I have some macros here. If there is a problem with displaying mathematical formulas, please run me to load these macros.</i></td></td></table>\n",
    "$ \\newcommand{\\bra}[1]{\\langle #1|} $\n",
    "$ \\newcommand{\\ket}[1]{|#1\\rangle} $\n",
    "$ \\newcommand{\\braket}[2]{\\langle #1|#2\\rangle} $\n",
    "$ \\newcommand{\\inner}[2]{\\langle #1,#2\\rangle} $\n",
    "$ \\newcommand{\\biginner}[2]{\\left\\langle #1,#2\\right\\rangle} $\n",
    "$ \\newcommand{\\mymatrix}[2]{\\left( \\begin{array}{#1} #2\\end{array} \\right)} $\n",
    "$ \\newcommand{\\myvector}[1]{\\mymatrix{c}{#1}} $\n",
    "$ \\newcommand{\\myrvector}[1]{\\mymatrix{r}{#1}} $\n",
    "$ \\newcommand{\\mypar}[1]{\\left( #1 \\right)} $\n",
    "$ \\newcommand{\\mybigpar}[1]{ \\Big( #1 \\Big)} $\n",
    "$ \\newcommand{\\sqrttwo}{\\frac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\dsqrttwo}{\\dfrac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\onehalf}{\\frac{1}{2}} $\n",
    "$ \\newcommand{\\donehalf}{\\dfrac{1}{2}} $\n",
    "$ \\newcommand{\\hadamard}{ \\mymatrix{rr}{ \\sqrttwo & \\sqrttwo \\\\ \\sqrttwo & -\\sqrttwo }} $\n",
    "$ \\newcommand{\\vzero}{\\myvector{1\\\\0}} $\n",
    "$ \\newcommand{\\vone}{\\myvector{0\\\\1}} $\n",
    "$ \\newcommand{\\vhadamardzero}{\\myvector{ \\sqrttwo \\\\  \\sqrttwo } } $\n",
    "$ \\newcommand{\\vhadamardone}{ \\myrvector{ \\sqrttwo \\\\ -\\sqrttwo } } $\n",
    "$ \\newcommand{\\myarray}[2]{ \\begin{array}{#1}#2\\end{array}} $\n",
    "$ \\newcommand{\\X}{ \\mymatrix{cc}{0 & 1 \\\\ 1 & 0}  } $\n",
    "$ \\newcommand{\\Z}{ \\mymatrix{rr}{1 & 0 \\\\ 0 & -1}  } $\n",
    "$ \\newcommand{\\Htwo}{ \\mymatrix{rrrr}{ \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} } } $\n",
    "$ \\newcommand{\\CNOT}{ \\mymatrix{cccc}{1 & 0 & 0 & 0 \\\\ 0 & 1 & 0 & 0 \\\\ 0 & 0 & 0 & 1 \\\\ 0 & 0 & 1 & 0} } $\n",
    "$ \\newcommand{\\norm}[1]{ \\left\\lVert #1 \\right\\rVert } $"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2>Superposition</h2>\n",
    "\n",
    "There is no classical counterpart of the concept \"superposition\".\n",
    "\n",
    "But, we can still use a classical analogy that might help us to give some intuitions.\n",
    "\n",
    "<h3> Probability distribution </h3>\n",
    "\n",
    "Suppose that Asja starts in $ \\myvector{1\\\\0} $ and secretly applies the probabilistic operator $ \\mymatrix{cc}{ 0.3 & 0.6 \\\\ 0.7 & 0.4 } $.\n",
    "\n",
    "Because she applies her operator secretly, our information about her state is probabilistic, which is calculated as\n",
    "\n",
    "$$\n",
    "    \\myvector{0.3 \\\\ 0.7} = \\mymatrix{cc}{ 0.3 & 0.6 \\\\ 0.7 & 0.4 } \\myvector{1\\\\0}.\n",
    "$$\n",
    "\n",
    "Asja is either in state 0 or in state 1.\n",
    "\n",
    "However, from our point of view, Asja is in state 0 and state 1 with probabilities $ 0.3 $ and $ 0.7 $, respectively.\n",
    "\n",
    "We can say that Asja in a probability distribution of states 0 and 1, being in both states at the same time.\n",
    "\n",
    "On the other hand, if we observe Asja's state, then our information about Asja becomes deterministic: either $ \\myvector{1 \\\\ 0} $ or $ \\myvector{0 \\\\ 1} $.\n",
    "\n",
    "We can say that after measurement the probabilistic state $ \\myvector{0.3 \\\\ 0.7}  $ collapses to either $ \\myvector{1 \\\\ 0} $ or $ \\myvector{0 \\\\ 1} $.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> The third experiment </h3>\n",
    "\n",
    "Now, we can explain the following experiment.\n",
    "\n",
    "<img src=\"../images/photon7.jpg\" width=\"65%\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<b> The initial Step </b>\n",
    "\n",
    "The photon is in state $ \\ket{v_0} = \\vzero $."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<b> The first step </b>\n",
    "\n",
    "Hadamard is applied:\n",
    "\n",
    "$ \\ket{v_1} = \\hadamard \\vzero = \\vhadamardzero $.\n",
    "\n",
    "At this point, the photon is in a <b>superposition</b> of state $ \\ket{0} $ and state $ \\ket{1} $, <u>being in both states with the amplitudes</u> $ \\frac{1}{\\sqrt{2}} $ and $ \\frac{1}{\\sqrt{2}} $, respectively.\n",
    "\n",
    "The state of photon is $ \\ket{v_1} = \\vhadamardzero $, and we can represent it also as follows:\n",
    "\n",
    "$ \\ket{v_1} =  \\frac{1}{\\sqrt{2}} \\ket{0} + \\frac{1}{\\sqrt{2}} \\ket{1} $."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<b> The second step </b>\n",
    "\n",
    "Hadamard is applied again:\n",
    "\n",
    "We write the affect of Hadamard on states $ \\ket{0} $ and $ \\ket{1} $ as follows:\n",
    "\n",
    "$ H \\ket{0} = \\frac{1}{\\sqrt{2}} \\ket{0} + \\frac{1}{\\sqrt{2}} \\ket{1} $\n",
    "\n",
    "$ H \\ket{1} = \\frac{1}{\\sqrt{2}} \\ket{0} - \\frac{1}{\\sqrt{2}} \\ket{1} $\n",
    "\n",
    "This representation helps us to see clearly why the state $ \\ket{1} $ disappears.\n",
    "\n",
    "Now, let's see the affect of Hadamard on the quantum state $ \\ket{v_1} =  \\frac{1}{\\sqrt{2}} \\ket{0} + \\frac{1}{\\sqrt{2}} \\ket{1} $:\n",
    "\n",
    "$ \\ket{v_2} = H \\ket{v_1} = H \\mybigpar{ \\frac{1}{\\sqrt{2}} \\ket{0} + \\frac{1}{\\sqrt{2}} \\ket{1} } = \\frac{1}{\\sqrt{2}} H \\ket{0} + \\frac{1}{\\sqrt{2}} H \\ket{1} $\n",
    "\n",
    "We can replace $ H\\ket{0} $ and $ H\\ket{1} $ as described above.\n",
    "\n",
    "$ \\ket{v_2} $ is formed by the summation of the following terms:\n",
    "\n",
    "$ \\frac{1}{\\sqrt{2}} H \\ket{0} = \\frac{1}{2} \\ket{0} + \\frac{1}{2} \\ket{1}  $\n",
    "\n",
    "$ \\frac{1}{\\sqrt{2}} H \\ket{1} = \\frac{1}{2} \\ket{0} - \\frac{1}{2} \\ket{1}  $\n",
    "\n",
    "<font color=\"blue\">The amplitude of $ \\ket{0} $ becomes 1, but the amplitude of $ \\ket{1} $ becomes 0 because of cancellation.</font>\n",
    "\n",
    "$ \\ket{v_2} = 1 \\cdot \\ket{0} + 0 \\cdot \\ket{1} = \\ket{0} $.\n",
    "\n",
    "The photon was in both states at the same time with <u>certain amplitudes</u>.\n",
    "\n",
    "After the second Hadamard, the results are <u>interfered with each other</u>.\n",
    "\n",
    "The interference can be constructive or destructive.\n",
    "\n",
    "In our examples, <font color=\"green\">the resulting $ \\ket{0} $s are interfered constructively</font>, but <font color=\"red\">the resulting $ \\ket{1} $s are interfered destructively</font>."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Observations </h3>\n",
    "\n",
    "<u>Probabilistic systems</u>: If there is a nonzero transition to a state, then it contributes to the probability of this state positively. \n",
    "\n",
    "<u>Quantum systems</u>: If there is a nonzero transition to a state, then we cannot make such an interpretation without knowing the other transtions to this state.\n",
    "\n",
    "If it is the only transition, then it contributes to the amplitude (and probability) of the state, and it does not matter whether the sign of the transition is positive or negative.\n",
    "\n",
    "If there is more than one transition, then depending on the summation of all transitions, we can determine whether a specific transition contributes or not.\n",
    "\n",
    "As a simple rule, if the final amplitude of the state and nonzero transition have the same sign, then it is a positive contribution; and, if they have the opposite signs, then it is a negative contribution."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Task 1 </h3>\n",
    "\n",
    "[on paper]\n",
    "\n",
    "Start in state $ \\ket{u_0} =  \\ket{1} $.\n",
    "\n",
    "Apply Hadamard operator to $ \\ket{u_0} $, i.e, find $ \\ket{u_1} = H \\ket{u_0} $.\n",
    "\n",
    "Apply Hadamard operator to $\\ket{u_1}$, i.e, find $ \\ket{u_2} = H \\ket{u_1} $.\n",
    "\n",
    "Observe the constructive and destructive interferences, when calculating $ \\ket{u_2} $."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Being in a superposition </h3>\n",
    "\n",
    "A quantum system can be in more than one state with nonzero amplitudes.\n",
    "\n",
    "Then, we say that our system is in a superposition of these states.\n",
    "\n",
    "When evolving from a superposition, the resulting transitions may affect each other constructively and destructively. \n",
    "\n",
    "This can happen only because of having both negative or positive amplitudes. \n",
    "\n",
    "Otherwise, all nonzero transitions are added up to each other as in probababilistic systems."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Measurement </h3>\n",
    "\n",
    "We can measure a quantum system, and then the system is observed in one of its states.\n",
    "\n",
    "This is the most basic measurement in quantum computing. \n",
    "\n",
    "(There are more generic measurement operators, but we will not cover them.)\n",
    "\n",
    "The probability of the system to be observed in a specified state is the square value of its amplitude.\n",
    "<ul>\n",
    "    <li> If the amplitude of a state is zero, then this state cannot be observed. </li>\n",
    "    <li> If the amplitude of a state is nonzero, then this state can be observed. </li>\n",
    "</ul>\n",
    "\n",
    "For example, if the system is in quantum state \n",
    "\n",
    "$$\n",
    "    \\myrvector{ -\\frac{\\sqrt{2}}{\\sqrt{3}} \\\\ \\frac{1}{\\sqrt{3}} },\n",
    "$$\n",
    "\n",
    "then, after a measurement, we can observe the system in state $\\ket{0} $ with probability $ \\frac{2}{3} $ and in state $\\ket{1}$ with probability $ \\frac{1}{3} $.\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h4> Collapsing </h4>\n",
    "\n",
    "After the measurement, the system collapses to the observed state, and so the system is no longer in a superposition.\n",
    "\n",
    "Thus, the information kept in a superposition is lost. \n",
    "\n",
    "In the above example, when the system is observed in state $\\ket{0}$, then the new state becomes $ \\myvector{1 \\\\ 0} $. \n",
    "\n",
    "If it is observed in state $\\ket{1}$, then the new state becomes $ \\myvector{0 \\\\ 1} $."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Task 2 </h3>\n",
    "\n",
    "We have a quantum system with four states: $\\ket{00}$, $ \\ket{01} $, $\\ket{10}$, and $ \\ket{11} $.\n",
    "\n",
    "We can also say that our system has two qubits.\n",
    "\n",
    "\n",
    "Suppose that the system is in the following state:\n",
    "\n",
    "$ \\myrvector{ \\dfrac{ 1 }{  \\sqrt{3} - \\sqrt{ 5 + 2\\sqrt{6}} } \\\\ \\\\ \\dfrac{1}{ \\sqrt{3} - \\sqrt{ 7 + 2\\sqrt{12} } } \\\\ \\\\\n",
    " \\dfrac{  1 }{ \\sqrt{5} - \\sqrt{  13 + 2\\sqrt{40} }  } \\\\ \\\\ \\dfrac{1}{ \\sqrt{ 7 } - \\sqrt{ 15 + 2 \\sqrt{56} } } }.  $\n",
    " \n",
    " Find the probability of observing the system in state $\\ket{00}$, $ \\ket{01} $, $\\ket{10}$, or $ \\ket{11} $.\n",
    " \n",
    " You may write a function to calculate the dominator of each fraction automatically, where its value is determined by three values $a$, $ b$, and $ c $ by assuming the form $  \\sqrt{a} - \\sqrt{b + 2 \\sqrt{c} } $.\n",
    " \n",
    " Verify that the total probability is 1 (or almost 1)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#\n",
    "# your solution is here\n",
    "#\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"..\\bronze-solutions\\B48_Superposition_Solutions.ipynb#task2\">click for our solution</a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Z-gate (operator) </h3>\n",
    "\n",
    "The indentity operator $ I = \\mymatrix{cc}{1 & 0 \\\\ 0 & 1} $ does not affect the computation.\n",
    "\n",
    "What about the following operator?\n",
    "\n",
    "$ Z = \\Z $.\n",
    "\n",
    "It is very similar to the identity operator.\n",
    "\n",
    "Consider the quantum state $ \\ket{u} = \\myvector{ \\frac{3}{5} \\\\ \\frac{4}{5} }  $.\n",
    "\n",
    "Let's calculate the new quantum state after appying $ Z $ to $ \\ket{u} $:\n",
    "\n",
    "$ \\ket{u'} = Z \\ket{u} =  \\Z  \\myvector{ \\frac{3}{5} \\\\ \\frac{4}{5} } =  \\myrvector{ \\frac{3}{5} \\\\ -\\frac{4}{5} } $."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The quantum states $ \\ket{u} $ and $ \\ket{u'} $ look similar. \n",
    "\n",
    "The probabilities of observing the state 0 and state 1 are the same when the system is in $ \\ket{u} $ or $ \\ket{u'} $.\n",
    "\n",
    "On the other hand, they are far away from each other as shown below: \n",
    "\n",
    "<img src=\"../images/Z_u.jpg\" width = \"40%\">\n",
    "\n",
    "For example, by applying Hadamard to each of them, the probability of observing the state 0 and state 1 may change (?)\n",
    "\n",
    "To observe this, we can do a simple experiment. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Task 3 </h3>\n",
    "\n",
    "Create a quantum ciruit with 5 qubits.\n",
    "\n",
    "Apply h-gate (Hadamard operator) to each qubit.\n",
    "\n",
    "Apply z-gate ($Z$ operator) to randomly picked qubits. (e.g., $ mycircuit.z(qreg[i]) $)\n",
    "\n",
    "Apply h-gate to each qubit \n",
    "\n",
    "Measure each qubit.\n",
    "\n",
    "Execute your program 1000 times.\n",
    "\n",
    "Compare the outcomes of the qubits affected by z-gates, and the outcomes of the qubits not affected by z-gates.\n",
    "\n",
    "Does z-gate change the outcome?\n",
    "\n",
    "Why?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's import all necessary objects and methods for quantum circuits\n",
    "from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, execute, Aer\n",
    "from qiskit.tools.visualization import matplotlib_circuit_drawer as drawer\n",
    "# let's import randrange for random choices\n",
    "from random import randrange\n",
    "\n",
    "#\n",
    "# your code is here\n",
    "#\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"..\\bronze-solutions\\B48_Superposition_Solutions.ipynb#task3\">click for our solution</a>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
